package com.invengo.testrfid.app.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;

public class WifeStatueUtils {

	private static ConnectivityManager manager;

	/**
	 * 检测网络状态
	 * 
	 * 0没有网络，1手机网络，2wife网络
	 * 
	 * @param context
	 * @return
	 */
	public static int getNetWorkStatue(Context context) {

		if (checkNetworkState(context)) {
			if (isWife()) {
				return 2;
			} else {
				return 1;
			}
		} else {
			return 0;
		}
	}

	/**
	 * 检测网络是否连接
	 * 
	 * @return
	 */
	public static boolean checkNetworkState(Context context) {
		boolean flag = false;
		// 得到网络连接信息
		manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		// 去进行判断网络是否连接
		if (manager.getActiveNetworkInfo() != null) {
			flag = manager.getActiveNetworkInfo().isAvailable();
		}
		return flag;

	}

	/**
	 * 判断是否是wife连接
	 */
	public static boolean isWife() {
		State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();
		if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isNetworkAvailable(Context context) {
		if (null == context) {
			return false;
		}
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkinfo = manager.getActiveNetworkInfo();
		if (networkinfo != null && networkinfo.isConnected()) {
			return true;
		}
		return false;
	}

}
